import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.File;

/**
 * <p>Title: FireWall Project. UI Module </p>
 * <p>Description: The TestUI run tests on the UI Module</p>
 * <p>Copyright: Copyright (c) 2006</p>
 * @author FW team
 * @version 1.0
 */
public class TestUI
{
	public static void main(String[] args)
	{
		System.out.println("Test UI Module"); 
		UI tmpUI = new UI();
		File tmpFile = null;
		Exception tmpException = null;
		

		try
		{
			// TEST #1 - change function, test of turning logger on 
			System.out.println("TEST #1 - change function");
			System.out.println("test of turning logger on. The iptable file obligate in current directory");
			try
			{
				tmpUI.change(true,tmpUI.iptablesFile);
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}

			tmpFile = new File(tmpUI.iptablesFile);
			if(tmpFile.exists())
			{
				System.out.println("Success TEST #1:");
				printFile(tmpUI.iptablesFile);
			}
			else
			{
				if(tmpException != null)
				{
					System.out.println("Failed TEST #1: exception occur: ");
					System.out.println(tmpException.toString());
				}
				else 
					System.out.println("Failed TEST #1: file not exist");
			}
			tmpException = null;
			
			// TEST #2 - change function, test of turning logger off
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #2 - change function");
			System.out.println("test of turning logger off. The iptable file obligate in current directory");
		
			try
			{
				tmpUI.change(false, tmpUI.iptablesFile);
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}

			tmpFile = new File(tmpUI.iptablesFile);
			if(tmpFile.exists())
			{
				System.out.println("Success TEST #2:");
				printFile(tmpUI.iptablesFile);
			}
			else
				if(tmpException != null)
				{
					System.out.println("Failed TEST #2: exception occur: ");
					System.out.println(tmpException.toString());
					tmpException = null;
				}
				else 
					System.out.println("Failed TEST #2: file not exist");

			tmpException = null;

			// TEST #3 - change function, test of file not exist
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #3 - change function");
			System.out.println("test of file not found. " +
			"This test obligate file test1FileNotFound.txt NOT exist in current directory");

			try
			{
				tmpUI.change(false, "test1FileNotFound.txt");
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Success TEST #3: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Failed TEST #3: the file not exist. " +
								"the function change should return FileNotFound exception");
			}
			tmpException = null;
			
			// TEST #4 - start function 
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #4 - start function");
			System.out.println("test of start command transfer to the command exec correctly ");

			try
			{
				tmpUI.start();
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #4: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #4");
			}
			tmpException = null;

			// TEST #5 - stop function 
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #5 - stop function");
			System.out.println("test of stop command transfer to the command exec correctly ");

			try
			{
				tmpUI.stop();
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #5: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #5");
			}

			// TEST #6 - remove function
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #6 - remove function ");
			System.out.println("test of remove command transfer to the command exec correctly.");

			try
			{
				tmpUI.remove(true, 1);
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #6: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #6");
			}
			tmpException = null;

			// TEST #7 - help function 
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #7 - help function");
			System.out.println("test of help command transfer to the command exec correctly ");

			try
			{
				tmpUI.help();
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #7: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #7");
			}
			tmpException = null;
			
			// TEST #8 - save function 
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #8 - save function");
			System.out.println("test of save command transfer to the command exec correctly ");

			try
			{
				tmpUI.save("");
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #8: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #8");
			}
			tmpException = null;
		
			// TEST #9 - load function 
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #9 - load function");
			System.out.println("test of load command transfer to the command exec correctly ");

			try
			{
				tmpUI.load(tmpUI.IPTblFileNameConst);
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #9: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #9");
			}
			tmpException = null;

			// TEST #10 - addMatchRuleDir function - in
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #10 - addMatchRuleDir function");
			System.out.println("test of addMatchRuleDir with direction in command transfer to the command exec correctly state logger is on ");

			try
			{
				tmpUI.log(true);
				tmpUI.addMatchRuleDir("80", "HTTP", true, true);
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #12: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #12");
			}
			tmpException = null;

			// TEST #11 - addMatchRuleDir function - out
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #11 - addMatchRuleDir function");
			System.out.println("test of addMatchRuleDir with direction out command transfer to the command exec correctly state logger is on ");

			try
			{
				tmpUI.log(true);
				tmpUI.addMatchRuleDir("80", "HTTP", true, false);
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #12: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #12");
			}
			tmpException = null;

			// TEST #12 - addMatchRule function - loggerON
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #12 - addMatchRule function");
			System.out.println("test of addMatchRule command transfer to the command exec correctly state logger is on ");

			try
			{
				tmpUI.log(true);
				tmpUI.addMatchRule("80", "HTTP", true);
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #12: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #12");
			}
			tmpException = null;
			
			// TEST #13 - addMatchRule function - logger off
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #13 - addMatchRule function");
			System.out.println("test of addMatchRule command transfer to the command exec correctly state logger is off ");

			try
			{
				tmpUI.log(false);
				tmpUI.addMatchRule("80", "HTTP", true);
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #13: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #13");
			}
			tmpException = null;
			
			// TEST #14 - addIPRule function - no index, logger on
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #14 - addIPRule function");
			System.out.println("test of addIPRule command transfer to the command exec correctly state logger is on and no index transfer ");

			try
			{
				tmpUI.log(true);
				tmpUI.addIPRule("10.0.0.1","100", true , true, -1);
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #14: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #14");
			}
			tmpException = null;
			

			// TEST #15 - addIPRule function - no index, logger off
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #15 - addIPRule function");
			System.out.println("test of addIPRule command transfer to the command exec correctly state logger is off, no index transfer");

			try
			{
				tmpUI.log(false);
				tmpUI.addIPRule("10.0.0.1","" , true , true, -1);
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #15: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #15");
			}
			tmpException = null;

			// TEST #16 - addIPRule function - with index, logger on
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #16 - addIPRule function");
			System.out.println("test of addIPRule command transfer to the command exec correctly state logger is on, index transfer ");

			try
			{
				tmpUI.log(true);
				tmpUI.addIPRule("10.0.0.1", "", true , true, 1);
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #16: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #16");
			}
			tmpException = null;
			
			// TEST #17 - addIPRule function - with index, logger off
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #17 - addIPRule function");
			System.out.println("test of addIPRule command transfer to the command exec correctly state logger is off, with index transfer");

			try
			{
				tmpUI.log(false);
				tmpUI.addIPRule("10.0.0.1", "" , true , true, 1);
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #17: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #17");
			}
			tmpException = null;

			// TEST #18 - clean function 
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #18 - clean function");
			System.out.println("test of clean command transfer to the command exec correctly ");

			try
			{
				tmpUI.clean();
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #18: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #18");
			}
			tmpException = null;

			// TEST #19 - IPtable function 
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #19 - IPtable function");
			System.out.println("test of IPtable command transfer to the command exec correctly ");

			try
			{
				tmpUI.IPTable(" -t  " + tmpUI.iptableTableDefaultConst + " -L  --line-numbers ");
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #19: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #19");
			}
			tmpException = null;
			
			// TEST #20 - list function 
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #20 - list function");
			System.out.println("test of list command transfer to the command exec correctly ");

			try
			{
				tmpUI.list();
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #20: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #20");
			}
			tmpException = null;
			
			// TEST #21 - log function 
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #21 - log function");
			System.out.println("test of log command transfer to the command exec correctly ");

			try
			{
				tmpUI.log(true);
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #21: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #21");
			}
			tmpException = null;

			// TEST #22 - readLog function 
			System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #22 - readLog function");
			System.out.println("test of readLog command transfer to the command exec correctly ");

			try
			{
				tmpUI.readLog("tmp_log.out");
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #22: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #22");
			}
			tmpException = null;

			// TEST #23 - cleanLog function 
			/*System.out.println("-------------------------------------------------------------");
			System.out.println("TEST #23 - cleanLog function");
			System.out.println("test of readLog command transfer to the command exec correctly ");
			System.out.println("The result should be: " +  "call ivgenia's clean logger function");

			try
			{
				tmpUI.list();
			}
			catch(Exception ex)
			{
				tmpException= new Exception(ex);
			}
			
			if (tmpException != null)
			{
				System.out.println("Falied TEST #23: exception occur: ");
				System.out.println(tmpException.toString());
			}
			else
			{
				System.out.println("Success TEST #23");
			}
			*/
		}
		catch(IOException ex)
		{
			System.out.println("File Not Found"); 
		}
	}
	
	private static void printFile(String p_fileName) throws FileNotFoundException, IOException
	{
		FileReader tmpFile;
		try
		{
			tmpFile = new FileReader(p_fileName);
			
			
		}
		catch(FileNotFoundException ex)
		{
			System.out.println("Can't read file found not found");
			return;
		}

		LineNumberReader linesfileReader = new LineNumberReader(tmpFile);

		System.out.println("#################################");
		System.out.println("Printing file: " + p_fileName);
		System.out.println("#################################");
				
		try
		{
			for (String line = null; (line = linesfileReader.readLine()) != null;)
			{
				System.out.println(line.toString());
			}
		}
		catch(IOException ex)
		{
			System.out.println("Exception eccour on reading file");
			return;
		}
	}
}